package br.com.sigeden.dao;

import br.com.sigeden.model.Envolvido;
import br.com.sigeden.model.Subcategoria;
import br.com.sigeden.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author David Amom <davidamom@gmail.com>
 */
public class SubcategoriaDAO {

    private Session sessao;
    private Transaction transacao;
    private List<Subcategoria> subcategorias;

    public List<Subcategoria> listarSubcategorias() {
        sessao = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = sessao.createCriteria(Subcategoria.class);
        this.subcategorias = criteria.list();

        sessao.close();
        return subcategorias;
    }

    public void adicionarSubcategoria(Subcategoria subcategoria) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();
            
            Subcategoria s = new Subcategoria();
            s.setNome(subcategoria.getNome());
            
            sessao.save(s);
            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void removerSubcategoria(Subcategoria subcategoria) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.delete(subcategoria);
            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }

    public void atualizarSubcategoria(Subcategoria subcategoria) {
        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();

            sessao.update(subcategoria);
            transacao.commit();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sessao.close();
        }
    }
}
